package com.sky.service.impl;

import com.alibaba.fastjson.JSON;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.*;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.UserService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import com.sky.vo.UserReportVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;


/**
 * @author iTaotao
 * @version 1.0
 * @Description 用户接口实现类
 * @createTime 2023年08月01日 15:39:00
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    private final DishMapper dishMapper;
    private final SetmealDishMapper setmealDishMapper;
    private final SetMealMapper setMealMapper;

    @Override
    public List<Category> selectCategoryList(CategoryDTO categoryDTO) {
        List<Category> categoryList = userMapper.selectCategoryList(categoryDTO.getType());
        return categoryList;
    }

    @Override
    public List<DishVO> selectDishList(Integer categoryId) {
//        ValueOperations valueOperations = redisTemplate.opsForValue();
//        String cacheKey = "dishes:" + categoryId;
//        String json = (String) valueOperations.get(cacheKey);
//        if (StringUtils.isNotBlank(json)) {
//            List<DishVO> dishVOS = JSON.parseArray(json, DishVO.class);
//            return dishVOS;
//        }

        List<DishVO> dishList = userMapper.selectDishList(categoryId);
        dishList.forEach(dishVO -> {
            List<DishFlavor> dishFlavorList = userMapper.selectFlavor(dishVO.getId());
            if (dishFlavorList != null && dishFlavorList.size() != 0) {
                dishVO.setFlavors(dishFlavorList);
            }

        });

//        String s = JSON.toJSONString(dishList);
//        valueOperations.set(cacheKey, s);
        return dishList;
    }

    @Override
    public List<Setmeal> selectSetMealList(Long categoryId) {
        List<Setmeal> setmealList = userMapper.selectSetMealList(categoryId);
        return setmealList;
    }

    @Override
    public List<DishItemVO> selectSetMealDish(Long id) {
        List<DishItemVO> dishItemVO = userMapper.selectSetMealDish(id);
        log.info("参数:{}", dishItemVO);
        return dishItemVO;
    }


    @Override
    public synchronized void add(ShoppingCartDTO shoppingCartDTO) {
        if (Objects.nonNull(shoppingCartDTO.getDishId())) {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            ShoppingCart cart = userMapper.selectOne(shoppingCart);

            Dish dish = dishMapper.selectDishById(shoppingCart.getDishId());

            if (Objects.nonNull(cart)) {
                Integer number = cart.getNumber();
                cart.setNumber(number + 1);
                cart.setAmount(new BigDecimal(cart.getNumber()).multiply(dish.getPrice()));
                userMapper.updateShopCart(cart);
            } else {
                cart = new ShoppingCart();
                cart.setUserId(BaseContext.getCurrentId());
                cart.setDishId(shoppingCartDTO.getDishId());
                cart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                cart.setNumber(1);
                cart.setCreateTime(LocalDateTime.now());
                cart.setName(dish.getName());
                cart.setAmount(dish.getPrice());
                cart.setImage(dish.getImage());
                userMapper.insertShopCart(cart);

            }
        }

//套餐
        if (Objects.nonNull(shoppingCartDTO.getSetmealId())) {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            ShoppingCart cart = userMapper.selectOne(shoppingCart);
            Setmeal setmeal = setMealMapper.selectSetMealById(shoppingCartDTO.getSetmealId());
            //  SetmealDish setmealDish = setmealDishMapper.selectBySetMealId(shoppingCartDTO.getSetmealId());
            if (Objects.nonNull(cart)) {
                Integer number = cart.getNumber();
                cart.setNumber(number + 1);
                cart.setAmount(new BigDecimal(cart.getNumber()).multiply(setmeal.getPrice()));
                userMapper.updateSetMealShopCart(cart);
            } else {


                cart = new ShoppingCart();
                cart.setName(setmeal.getName());
                cart.setUserId(BaseContext.getCurrentId());
                cart.setSetmealId(shoppingCartDTO.getSetmealId());
                cart.setNumber(1);
                cart.setAmount(setmeal.getPrice());
                cart.setImage(setmeal.getImage());
                cart.setCreateTime(LocalDateTime.now());
                userMapper.insertSetMealCart(cart);

            }


        }
    }

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = userMapper.selectList(shoppingCart);
        return shoppingCartList;
    }

    @Override
    public void clean() {
        Long currentId = BaseContext.getCurrentId();
        userMapper.clean(currentId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
//菜品删除
        if (Objects.nonNull(shoppingCartDTO.getDishId())) {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            ShoppingCart shopping = userMapper.selectOne(shoppingCart);
            log.info(shopping.toString());
            Dish dish = dishMapper.selectDishById(shoppingCartDTO.getDishId());
            if (Objects.nonNull(shopping)) {
                Integer number = shopping.getNumber();
                if (number == 1) {
                    userMapper.deleteNumber(shoppingCart.getDishId());
                }
                if (number > 1) {
                    shopping.setNumber(number - 1);
                    shopping.setAmount(new BigDecimal(shopping.getNumber()).multiply(dish.getPrice()));
                    userMapper.updateNum(shopping);
                }
            }


        }
//套餐删除
        if (Objects.nonNull(shoppingCartDTO.getSetmealId())) {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            ShoppingCart shopping = userMapper.selectOne(shoppingCart);
            Setmeal setmeal = setMealMapper.selectSetMealById(shoppingCartDTO.getSetmealId());

            if (Objects.nonNull(shopping)) {
                Integer number = shopping.getNumber();
                if (number == 1) {
                    userMapper.deleteOne(shopping.getSetmealId());
                }
                if (number > 1) {
                    shopping.setNumber(number - 1);
                    shopping.setAmount(new BigDecimal(shopping.getNumber()).multiply(setmeal.getPrice()));
                    userMapper.updateNum(shopping);

                }
            }

        }
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {

        UserReportVO userReportVO = new UserReportVO();
        return userReportVO;
    }
}
